
package cup;

import cup.common.constant.CommonConstant;
import cup.core.launch.CupApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 启动器
 *
 * @author
 */
@EnableScheduling
@SpringBootApplication
@EnableConfigurationProperties()
@EnableAsync //开启异步
public class Application {
	private static final Logger log = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		ConfigurableApplicationContext run = CupApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
		ConfigurableEnvironment env = run.getEnvironment();
		log.info("swagger地址:\thttp://127.0.0.1:{}/doc.html", env.getProperty("server.port"));
	 }

	/**
	 * 线程池
	 *
	 * @param i  core pool
	 * @param i2 max pool
	 * @param i3 queue
	 * @param s  name
	 * @return new ThreadPoolTaskExecutor
	 */
	private Executor getExecutor(int i, int i2, int i3, String s) {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(i);
		executor.setMaxPoolSize(i2);
		executor.setQueueCapacity(i3);
		executor.setThreadNamePrefix(s);
		executor.initialize();
		return executor;
	}

	/**
	 * 异常信息告警异步线程池
	 */
	@Bean
	public Executor userLoginLogExecutor() {
		return getExecutor(2, 2, 500, "User_Login_Log_");
	}

	/**
	 * 处理第三方推送的检测设备的数据
	 */
	@Bean
	public Executor handlePushData() {
		return getExecutor(2, 2, 1000, "HandlePushData_");
	}

	/**
	 * 处理第三方推送数据到预警规则的逻辑
	 */
	@Bean
	public Executor handlePushWaringData(){
		return getExecutor(2, 2, 1000, "HandlePushWaring_");
	}

	/**
	 * 处理第三方推送数据到内涝点的逻辑
	 */
	@Bean
	public Executor handlePushPondData(){
		return getExecutor(2, 2, 1000, "HandlePushPondData");
	}

	/**
	 * 大屏线程池
	 */
	@Bean
	public Executor handleResourceData(){
		return getExecutor(5, 10, 1000, "HandleResourceData");
	}

	/**
	 * neo4j异步相关
	 */
	@Bean
	public Executor handleNeo4jData(){
		return getExecutor(8, 8, 500000, "HandleNeo4jData");
	}

	/**
	 * 重庆住建t0数据推送记录
	 */
	@Bean
	public Executor handleT0PushData(){
		return getExecutor(5, 5, 5000, "HandleT0PushData_");
	}

	@Bean
	public Executor saveCounty(){
		return getExecutor(50, 50, 50000, "saveCounty");
	}
}

